import turtle
import random
import simpleaudio as sa
import pygame

# 设置背景音乐
pygame.mixer.init()
track = pygame.mixer.music.load('./bgm/Casual Friday.mp3')
pygame.mixer.music.set_volume(0.8) # 设置音量大小 0.0 ~ 1.0
pygame.mixer.music.play()
 # 设置循环次数
pygame.mixer.music.play(loops=10000, start=0.0) 
# 引入音乐
laser = sa.WaveObject.from_wave_file('./bgm/laser.wav')
exp = sa.WaveObject.from_wave_file('./bgm/explosion.wav')

# 设置背景图片和标题
game = turtle.Screen()
game.setup(700, 600)
game.title("太空侵略者")
game.bgpic('./image/bg.gif')

#设置玩家
turtle.addshape('./image/player.gif')
player = turtle.Turtle()
# 隐藏起来跳动的画面
player.ht() # 隐藏
player.speed(0)  # 速度最快
player.up()  # 隐藏中间那条线
player.shape('./image/player.gif')
player.setpos(0, -250)
player.st() #显示


player_step = 15 # 步长
def go_left():
    x = player.xcor()
    x = x - player_step
    if x < -300:
        x = -300
    player.setx(x)

def go_right():
    x = player.xcor()
    x = x + player_step
    if x > 300:
        x = 300
    player.setx(x)


# 事件响应
turtle.listen()
turtle.onkey(go_left, 'Left')
turtle.onkey(go_right, 'Right')


# 添加子弹
boomb = turtle.Turtle()
boomb.ht()
boomb.speed(0)
boomb.up()
boomb.shape('triangle')
boomb.color('yellow')
boomb.shapesize(0.5, 0.5)
boomb.seth(90)

# 添加分数
score = 0
pen = turtle.Turtle()
pen.color('white')
pen.speed(0)
pen.up()
pen.ht()
pen.setpos(-340, 230)
score_string = f"分数：{score}"
pen.write(score_string, align="left", font=('Arial', 18, 'normal'))



# 发射子弹
is_fired = False
def fire():
    global is_fired
    if is_fired == False:
        boomb.setpos(player.xcor(), player.ycor() + 20)
        boomb.st()
        is_fired = True
        laser.play()
turtle.onkey(fire, 'space')


# 创建敌人
num = 6
inv_list = []
turtle.addshape('./image/inv.gif')
for i in range(6):
    inv = turtle.Turtle()
    inv_list.append(inv)
    inv.ht()
    inv.speed(0)
    inv.up()
    inv.shape('./image/inv.gif')
    x = random.randint(-200, 200)
    y = random.randint(100, 220)
    inv.setpos(x, y)
    inv.st()


#敌人动起来
inv_step = 2
go_back = False
boomb_step = 20
game_over = False
while True:
    if game_over:
        pen2 = turtle.Turtle()
        pen2.color('red')
        pen2.ht()
        pen2.write('GAME OVER!', align='center', font=('Arial',34,'normal'))
        break

    # 敌人移动
    for inv in inv_list:
        x = inv.xcor()
        x += inv_step
        inv.setx(x)
        if x > 300 or x < -300:
           go_back = True
        # 得分
        if inv.distance(boomb) < 15:
            exp.play()
            inv.setpos(0, 240)
            is_fired = False
            boomb.setpos(-300, -300)
            boomb.ht()
            score += 10
            score_string = f"分数：{score}"
            pen.clear()
            pen.write(score_string, align="left", font=('Arial', 18, 'normal'))
        # 游戏结束
        if inv.ycor()< -200:
            game_over = True


    if go_back:
        inv_step *= -1
        go_back = False
        for inv in inv_list:
            y = inv.ycor()
            y -= 40
            inv.sety(y)

    if is_fired:
        # 子弹动起来
        y = boomb.ycor()
        y += boomb_step
        boomb.sety(y)
        if y > 250:
            is_fired = False
            boomb.setpos(-300, -350)
            boomb.ht()

game.mainloop()